﻿/*
给定一个二叉树和一个目标和，找到所有从根节点到叶子节点路径总和等于给定目标和的路径。
说明: 叶子节点是指没有子节点的节点。

示例:
给定如下二叉树，以及目标和 sum = 22，

			  5
			 / \
			4   8
		   /   / \
		  11  13  4
		 /  \    / \
		7    2  5   1
返回:

[
   [5,4,11,2],
   [5,8,4,5]
]
*/
#include "AllInc.h"

struct TreeNode
{
	int val;
	TreeNode *left;
	TreeNode *right;
	TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

class Solution
{
public:
	vector<vector<int>> pathSum(TreeNode* root, int sum) 
	{
		vector<int> path;
		DoPath(root, sum, path);
		return result;
	}

	void DoPath(TreeNode* root, int sum, vector<int>& path)
	{
		if (NULL == root)
		{
			return;
		}
		path.push_back(root->val);
		if (root->val == sum && root->left == NULL && root->right == NULL)
		{
			result.push_back(path);
			path.pop_back();
			return;
		}
		sum -= root->val;
		DoPath(root->left, sum, path);
		DoPath(root->right, sum, path);
		path.pop_back();
	}

	vector<vector<int>> result;
};

//int main()
//{
//	Solution s;
//	TreeNode n1(5);
//	TreeNode n2(4);
//	TreeNode n3(8);
//	TreeNode n4(11);
//	TreeNode n5(13);
//	TreeNode n6(4);
//	TreeNode n7(7);
//	TreeNode n8(2);
//	TreeNode n9(5);
//	TreeNode n10(1);
//	n1.left = &n2;
//	n1.right = &n3;
//	n2.left = &n4;
//	n3.left = &n5;
//	n3.right = &n6;
//	n4.left = &n7;
//	n4.right = &n8;
//	n6.left = &n9;
//	n6.right = &n10;
//	auto result = s.pathSum(&n1, 22);
//
//	return 0;
//}